package com.gzyj.jtt808.v2011.protocol.subiao;


import com.gzyj.jtt808.utils.HexBytesUtil;
import com.gzyj.jtt808.v2011.additionalinfo.JTT808_AddtionalInfo;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Data
@ApiModel("变道决策辅助系统报警")
public class JTT808_0x0200_0x67 extends JTT808_AddtionalInfo implements Serializable {


    @ApiModelProperty("报警ID")
    public Long AlarmId;

    @ApiModelProperty("标志状态")
    public Byte FlagState;

    @ApiModelProperty("报警/事件类型")
    public Byte AlarmOrEventType;

    @ApiModelProperty("报警/事件类型 解析后")
    public String AlarmOrEventTypeString;


    @ApiModelProperty("车速")
    public Byte Speed;

    @ApiModelProperty("高程")
    public Integer Altitude;

    @ApiModelProperty("纬度")
    public Long Latitude;

    @ApiModelProperty("经度")
    public Long Longitude;
    /// <summary>
    ///
    /// YYMMDDhhmmss
    /// BCD[6]
    /// </summary>
    @ApiModelProperty("日期时间")
    public Date AlarmTime;

    @ApiModelProperty("车辆状态")
    public Integer VehicleState;

    @ApiModelProperty("报警标识号")
    public AlarmIdentificationProperty AlarmIdentification;

    public String AlarmIdentificationStr;


    public JTT808_0x0200_0x67() {
        super(0x67);
        this.setLength(41);
    }

    @Override
    public void encode(ByteBuf buf) {
        buf.writeByte(this.getID());
        buf.writeByte(this.getLength());

        buf.writeInt(this.AlarmId.intValue());
        buf.writeByte(this.FlagState);
        buf.writeByte(this.AlarmOrEventType);

        buf.writeByte(this.Speed);
        buf.writeShort(this.Altitude);
        buf.writeInt(this.Latitude.intValue());
        buf.writeInt(this.Longitude.intValue());
        buf.writeBytes(HexBytesUtil.WriteDateTime_yyMMddHHmmss(this.AlarmTime));
        buf.writeShort(this.VehicleState);
        this.AlarmIdentification.encode(buf);
    }

    @Override
    public JTT808_0x0200_0x67 decode(ByteBuf buf) {
        this.setLength(buf.readByte());

        this.AlarmId = buf.readUnsignedInt();
        this.FlagState = buf.readByte();
        this.AlarmOrEventType = buf.readByte();
        switch (this.AlarmOrEventType) {
            case 0x01:
                this.AlarmOrEventTypeString = "后方接近报警";
                break;
            case 0x02:
                this.AlarmOrEventTypeString = "左侧后方接近报警";
                break;
            case 0x03:
                this.AlarmOrEventTypeString = "右侧后方接近报警";
                break;
        }

        this.Speed = buf.readByte();
        this.Altitude = buf.readUnsignedShort();

        this.Latitude = buf.readUnsignedInt();
        this.Longitude = buf.readUnsignedInt();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            this.AlarmTime = sdf.parse(HexBytesUtil.ReadDateTime_yyMMddHHmmss(HexBytesUtil.readByOffset(buf, 6)));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        this.VehicleState = buf.readUnsignedShort();

        this.AlarmIdentification = new AlarmIdentificationProperty();
        this.AlarmIdentification.decode(buf);

        ByteBuf tmp= Unpooled.buffer(16);
        this.AlarmIdentification.encode(tmp);
        this.AlarmIdentificationStr= ByteBufUtil.hexDump(tmp);

        return this;
    }
}
